package com.cndw.kungfu;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JDBC {
	
	private static final String driver = "com.mysql.jdbc.Driver"; 
	private static final String dbName_s = "kungfu_qq"; 
	private static final String dbName_t = "kungfu_sk"; 
	private static final String userName = "kungfu_user"; 
	private static final String passwrod = "kunfudebug"; 
	private static final String url_s = "jdbc:mysql://192.168.22.2/" + dbName_s + "?useUnicode=true&autoReconnect=true&failOverReadOnly=false&characterEncoding=utf8&rewriteBatchedStatements=true";
	private static final String url_t = "jdbc:mysql://192.168.22.2/" + dbName_t + "?useUnicode=true&autoReconnect=true&failOverReadOnly=false&characterEncoding=utf8&rewriteBatchedStatements=true";
	
	private static Connection conn = null;
	private static PreparedStatement ps = null;
	
	public static void connect(int type) {
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(type == 1 ? url_s : url_t, userName, passwrod); 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static ResultSet excute(String sql) {
		ResultSet resultSet = null;
		try {
			ps = conn.prepareStatement(sql); 
			resultSet = ps.executeQuery();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultSet; 
	}
	
	public static void close() {
		try {
			if(ps != null) {
				ps.close();
				ps = null;
			}
			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
